package com.gdpu.controller;

import com.gdpu.api.CommonResult;
import com.gdpu.config.JwtConfig;
import com.gdpu.dto.UmsMemberParam;
import com.gdpu.model.UmsMember;
import com.gdpu.service.UmsMemberService;
import com.gdpu.vo.UmsMemberInfoVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author:weiming
 * @Date:2021/5/27
 */
@Api(tags = "用户模块")
@RestController
@RequestMapping("/member")
public class UmsMemberController {
    @Autowired
    private UmsMemberService umsMemberService;
    @Autowired
    private JwtConfig jwtConfig;
    @PostMapping("/doLogin")
    CommonResult doLogin(@RequestBody UmsMemberParam umsMemberParam){

        String token = umsMemberService.login(umsMemberParam.getUsername(), umsMemberParam.getPassword());
        if (token == null) {
            return CommonResult.failed("用户名或密码错误");
        }
        Map<String, Object> map = new HashMap<>(3);
        map.put("token",token);
        map.put("tokenHead", jwtConfig.tokenHead);
        return CommonResult.success(map, "登录成功");
    }

    @GetMapping("/info/")
    public CommonResult info(){
        UmsMemberInfoVo userInfo = umsMemberService.getUserInfo();
        Map<String, Object> map = new HashMap<>(1);
        map.put("userInfo", userInfo);
        return  CommonResult.success(map);
    }

    private UmsMemberInfoVo getUserInfo(){
        UmsMemberInfoVo userInfo = umsMemberService.getUserInfo();
        return  userInfo;
    }

    @ApiOperation("用户账户余额")
    @GetMapping("/account")
    CommonResult account(){
        UmsMemberInfoVo userInfo = umsMemberService.getUserInfo();
        HashMap<String, Object> map = new HashMap<>(1);
        map.put("userInfo",userInfo);
        return  CommonResult.success(map);
    }


    @ApiOperation("用户注册")
    @PostMapping("/regist")
    CommonResult regist(@RequestBody UmsMemberParam umsMemberParam){
        int count  = umsMemberService.regist(umsMemberParam.getUsername(), umsMemberParam.getPassword());
        if ( count != 0){
          return   CommonResult.success("注册成功");
        }
        return CommonResult.failed("注册失败");
    }



}
